import { createRouter, createWebHistory } from 'vue-router';
import { setUserInfo, getUserToken, getGotoRouteName } from '../utils/user';
import store from '../store/store';
import beforeRoutes from './beforeRoutes';
import afterRoutes from './afterRoutes';
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [...beforeRoutes, ...afterRoutes]
});
/**
 * 动态设置路由（在登录后调用setUserInfo）
 */
export function addRoute() {
    const routeList = [
        ...store.state.user.menuRoutes,
        ...afterRoutes
    ];
    routeList.forEach(item => {
        router.addRoute(item);
    });
}
router.beforeEach(async (to) => {
    if (to.name === 'login') return true;
    if (store.state.user.isLogin === true) return true;
    if (getUserToken()) {
        await setUserInfo();
        addRoute();
        if (to.fullPath === '/') return { name: getGotoRouteName() || 'login' };
        return to.fullPath;
    };
    return '/login';
});
export default router;